﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Cinchoo.Core.IO;
using Cinchoo.Core.Collections;
using System.IO;
using System.Collections;

namespace Cinchoo.Core.ETL
{
    public class ChoETLCSVFileImportTask : ChoETLTask
    {
        #region Instance Data Members (Private)

        protected string _filePath;
        protected string _seperator = ",";

        #endregion Instance Data Members (Private)

        #region Constructors

        public ChoETLCSVFileImportTask(string filePath = null, string seperator = null)
        {
            _filePath = filePath;
            if (!seperator.IsNullOrWhiteSpace())
                _seperator = seperator;
        }

        #endregion Constructors

        #region ChoETLFileLoadTask Overridables

        protected override bool Initialize()
        {
            ChoGuard.ArgumentNotNullOrEmpty(_filePath, "FilePath");

            if (_filePath.IsNullOrWhiteSpace())
            {
                //object[] objs = ChoETLRuntime.Split2Objects(ChoString.ExpandPropertiesEx(_filePath));
                //if (!ChoGuard.IsArgumentNotNullOrEmpty(objs) || !(objs[0] is string))
                throw new ChoETLException("{0} is not a valid file path.".FormatString(_filePath));
                //else
                //    _filePath = ChoPath.GetFullPath(objs[0] as string);
            }
            return base.Initialize();
        }

        protected override IEnumerable<IEnumerable<ChoETLObject>> ExecuteTask(IEnumerable<IEnumerable<ChoETLObject>> lists)
        {
            yield return new ChoEnumerable<ChoETLScalarObject>(new ChoCSVFileReadEnumerator(_filePath, _seperator));
        }

        #endregion ChoETLFileLoadTask Overridables

        private class ChoCSVFileReadEnumerator : IEnumerator<ChoETLScalarObject>
        {
            private readonly string _filePath;
            private readonly string _seperator;
            private StreamReader _sr;
            private string _line;

            public ChoCSVFileReadEnumerator(string filePath, string seperator)
            {
                _filePath = filePath;
                _seperator = seperator;
                Reset();
            }

            public ChoETLScalarObject Current
            {
                get {
                    int i = 0;
                    return new ChoETLScalarObject(
                    (from item in _line.Split(_seperator)
                    select new KeyValuePair<string, object>(i++.ToString(), item)).ToArray()); }
            }

            public void Dispose()
            {
                _sr.Dispose();
            }

            object IEnumerator.Current
            {
                get { return Current; }
            }

            public bool MoveNext()
            {
                _line = _sr.ReadLine();
                if (_line == null)
                    return false;
                else
                    return true;
            }

            public void Reset()
            {
                _sr = new StreamReader(_filePath);
            }
        }
    }

}
